/**
 * 
 */
package it.agm.fanta.asynctask;

import it.agm.activity.dialog.GenericDialog;
import it.agm.fanta.activity.LoginActivity;
import it.agm.fanta.asynctask.jersey.patch.AndroidServiceIteratorProvider;
import it.agm.fanta.exception.ConnectionException;
import it.agm.fanta.FantaserviceApp;
import it.agm.fanta.R;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jersey.spi.service.ServiceFinder;

/**
 * @author AGM
 * 
 */
public abstract class SyncAsyncTask extends AsyncTask<Void, Void, Void> {

	protected ProgressDialog progress;
	protected Context context;
	protected View rootView;
	protected MenuItem refreshMenuItem;
	protected WeakReference<LoginActivity> meinActivityWeakRef;
	public Dialog mdialog;
	protected Intent intent;

	public SyncAsyncTask() {
	}

	@SuppressWarnings("rawtypes")
	public SyncAsyncTask(Context context, MenuItem refreshMenuItem) {
		ServiceFinder.setIteratorProvider(new AndroidServiceIteratorProvider());
		this.context = context;
		this.refreshMenuItem = refreshMenuItem;
	}

	@Override
	protected void onPreExecute() {
		if (refreshMenuItem != null) {
			refreshMenuItem.setActionView(R.layout.action_progressbar);

			refreshMenuItem.expandActionView();
		}

	}

	@Override
	protected Void doInBackground(Void... params) {
		ObjectMapper mapper = new ObjectMapper();
		try {
			setData(mapper);
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ConnectionException e) {
			meinActivityWeakRef.get().runOnUiThread(new Runnable() {
				public void run() {
					new GenericDialog(meinActivityWeakRef.get(), context
							.getResources()
							.getString(R.string.alert_connection), "WARN");
				}
			});
		}

		return null;
	}

	/**
	 * 
	 * @param holder
	 * @param httpclient
	 * @param httpost
	 * @param mapper
	 * @return
	 * @throws JSONException
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws ClientProtocolException
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 */
	public abstract boolean setData(ObjectMapper mapper) throws JSONException,
			UnsupportedEncodingException, IOException, ClientProtocolException,
			JsonParseException, JsonMappingException, ConnectionException;
}
